<?php
declare (strict_types = 1);

namespace app\index\controller;

// use think\Request;
use think\facade\Db;
use think\facade\Cache;
use think\facade\Request;
use think\facade\Config;

class Apply{
    
    public function tab(){
        if(Request::isPost()){
            $openId=input('param.openId','');
            $data=Db::table('apply')->where([
                ['openid','=',$openId],
                ['booking_date','>',date('Y-m-d 00:00:00',strtotime('-1 year'))]
            ])->column('booking_date');
            $progress=0;
            $over=0;
            foreach($data as $k=>$v){
                if($v>=date('Y-m-d')){
                    $progress=$progress+1;
                }else{
                    $over=$over+1;
                }
            }
            $nav=[['name'=>'进行中','badge'=>['value'=>$progress]],['name'=>'已结束','badge'=>['value'=>$over]]];
            return json(['code'=>200,'message'=>'获取成功','success'=>true,'result'=>['list'=>$nav]]);
        }
    }
    //获取列表
    public function getListByopenId(){
        if(Request::isPost()){
            $openId=input('param.openId','');
            $accountId=input('param.accountId',0);
            $page=input('param.page',1);
            $size=input('param.size',10);
            $over=input('param.over',0);
            $where=[['ay.openid','=',$openId]];
            if($over==1){
                $where[]=['ay.booking_date','<',date('Y-m-d')];
            }else{
                $where[]=['ay.booking_date','>=',date('Y-m-d')];
            }
            $data=Db::table('apply ay')
            ->field('ay.id as applyId,ay.mobile,ay.true_name,ay.nickname,ay.booking_date,ay.accompany,ay.remark')
            ->field('a.title as activityTitle')
            ->join('activity a','ay.activity_id=a.id')
            ->where($where)
            ->order('ay.booking_date desc,ay.create_at desc')
            ->paginate([
                'list_rows' => $size,
                'page'      => $page
            ]);
            $total=$data->total();
            $data=$data->items();
            foreach($data as $k=>$v){
                $data[$k]['status']['text']='进行中';
                $data[$k]['status']['color']='#5ac725';
                
                if(strtotime($v['booking_date'].' 18:00:00')<time()){
                    $data[$k]['status']['text']='已结束';
                    $data[$k]['status']['color']='#c6c7cb';
                }

            }
            $page_count=ceil($total/$size);
            return json(['code'=>200,'message'=>'获取成功','success'=>true,'result'=>['list'=>$data,'total'=>$total,'page'=>['current'=>$page,'size'=>$size,'total'=>$page_count]]]);
        }
    }
    
    public function getApplyById(){
        if(Request::isPost()){
            $applyId=input('param.applyId',0);
            $data=Db::table('apply ay')
            ->field('ay.id as applyId,ay.mobile,ay.true_name,ay.nickname,ay.booking_date,ay.accompany,ay.remark')
            ->field('a.title as activityTitle,a.cover')
            ->join('activity a','ay.activity_id=a.id')
            ->where(['ay.id'=> $applyId])
            ->find();
            $cover=json_decode($data['cover'],true);
            $data['cover']=isset($cover[0])?'https://darong.storage.1415926.com.cn'.$cover[0]:'https://uviewui.com/swiper/swiper1.png';
            $data['status']['text']='进行中';
            $data['status']['color']='#5ac725';
            
            if(strtotime($data['booking_date'].' 18:00:00')<time()){
                $data['status']['text']='已结束';
                $data['status']['color']='#c6c7cb';
            }
            return json(['code'=>200,'message'=>'获取成功','success'=>true,'result'=>$data]);

        }
    }
}